package com.sky.advice;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;


/*
*   按照框架要求编写元数据对象处理器，
*   在此类中统一为公共字段赋值，此类需要实现MetaObjectHandler接口
*
* */
@Component
@Slf4j
public class AutoFilMetaObjectHandler implements MetaObjectHandler {

    // 插入时自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("mp插入自动注入启动");
        this.setFieldValByName(AutoFillConstant.SET_CREATE_TIME,LocalDateTime.now(),metaObject);
        this.setFieldValByName(AutoFillConstant.SET_CREATE_USER,BaseContext.getCurrentId(),metaObject);
        this.setFieldValByName(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.now(),metaObject);
        this.setFieldValByName(AutoFillConstant.SET_UPDATE_USER,BaseContext.getCurrentId(),metaObject);
    }

    // 更改时自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("mp更改自动注入启动");
        this.setFieldValByName(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.now(),metaObject);
        this.setFieldValByName(AutoFillConstant.SET_UPDATE_USER,BaseContext.getCurrentId(),metaObject);
    }
}
